import requests
from untils import extract_text_from_font

cookies = {
    'ttwid': '1%7C8QuYJkQV3Lrasuxsla6nBmt1jQxj00mHSULbzB2b5FY%7C1733378817%7Ce22546c966f2ec46e4e9c99fca1f90e5aaa0fea386c8e26033b15525c2a3d774',
    'tt_webid': '7444805164559451672',
    'tt_web_version': 'new',
    'is_dev': 'false',
    'is_boe': 'false',
    '_gid': 'GA1.2.1008691453.1733378815',
    '_gat_gtag_UA_138671306_1': '1',
    '_ga': 'GA1.1.937963770.1733378815',
    '_ga_YB3EWSDTGF': 'GS1.1.1733378565.3.1.1733378815.60.0.0',
    'Hm_lvt_3e79ab9e4da287b5752d8048743b95e6': '1733210312,1733378566',
    'Hm_lpvt_3e79ab9e4da287b5752d8048743b95e6': '1733378816',
    'city_name': '%E6%9D%AD%E5%B7%9E',
}

headers = {
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'content-type': 'application/x-www-form-urlencoded',
    # 'cookie': 'ttwid=1%7C8QuYJkQV3Lrasuxsla6nBmt1jQxj00mHSULbzB2b5FY%7C1733378817%7Ce22546c966f2ec46e4e9c99fca1f90e5aaa0fea386c8e26033b15525c2a3d774; tt_webid=7444805164559451672; tt_web_version=new; is_dev=false; is_boe=false; _gid=GA1.2.1008691453.1733378815; _gat_gtag_UA_138671306_1=1; _ga=GA1.1.937963770.1733378815; _ga_YB3EWSDTGF=GS1.1.1733378565.3.1.1733378815.60.0.0; Hm_lvt_3e79ab9e4da287b5752d8048743b95e6=1733210312,1733378566; Hm_lpvt_3e79ab9e4da287b5752d8048743b95e6=1733378816; city_name=%E6%9D%AD%E5%B7%9E',
    'origin': 'https://www.dongchedi.com',
    'priority': 'u=1, i',
    'referer': 'https://www.dongchedi.com/usedcar/x-x-x-x-x-x-1-x-x-x-x-x-x-x-x-x-x-x-x-x-x-330100-1-x-x-x-x-x',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'x-forwarded-for': '',
}

params = {
    'aid': '1839',
    'app_name': 'auto_web_pc',
}

data = '&sh_city_name=全国&page=1&limit=20'.encode()
font_map = extract_text_from_font('./96fc7b50b772f52-700.woff2')
response = requests.post(
    'https://www.dongchedi.com/motor/pc/sh/sh_sku_list',
    params=params,
    cookies=cookies,
    headers=headers,
    data=data,
)


def covert_data(data):
    s = ""
    for i in data:
        if i != ".":
            i = font_map[repr(i)[3:-1]]
        s += i

    return s


def covert_data2(data):
    a, b = data.split("|")
    return covert_data(a.strip()) + covert_data(b.strip())


for i in response.json()['data']['search_sh_sku_info_list']:
    a = {
        'sub_title': covert_data2(i['sub_title']),
        'official_price': covert_data(i['official_price']),
        'sh_price': covert_data(i['sh_price']),
    }
    print(a)
